<import template="/panel/_header" />
<div class="page-container">
    <div class="main-content">
        <div class="page-title">

            <div class="title-env">
                <h1 class="title">修改密码</h1>
                <p class="description">修改网站登录密码, 请妥善保管</p>
            </div>

            <div class="breadcrumb-env">
                <ol class="breadcrumb bc-1">
                    <li>
                        <a href="/member"><i class="fa-home"></i>主页</a>
                    </li>
                    <li>
                        <a href="javascript:;">我的信息</a>
                    </li>
                    <li class="active">
                        <strong>修改密码</strong>
                    </li>
                </ol>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12 col-md-6 col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <i class="fa fa-angle-right"></i>&nbsp;网站登录密码
                    </div>
                    <div class="panel-body">
                        <div class="form-group">
                            <label for="nowpwd" class="control-label">旧密码：</label>
                            <input type="password" class="form-control" id="nowpwd" placeholder="旧密码">
                            <label for="pwd" class="control-label">新密码：</label>
                            <input type="password" class="form-control" id="pwd" placeholder="新密码">
                            <label for="repwd" class="control-label">确认新密码：</label>
                            <input type="password" class="form-control" id="repwd" placeholder="再次输入新密码">
                        </div>
                    </div>
                    <button class="btn btn-info btn-block" id="submit-pwd">确定修改</button>
                </div>
            </div>
            <!-- change sspwd -->
            <div class="col-sm-12 col-md-6 col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <i class="fa fa-angle-right"></i>&nbsp;SS连接密码
                    </div>
                    <div class="panel-body">
                        <div class="form-group">
                            <label for="sspwd" class="control-label">旧连接密码：</label>
                            <input type="text" class="form-control" id="old_sspwd" value="{$user->sspwd}" placeholder="" disabled="disabled">
                            <label for="sspwd" class="control-label">SS连接密码：</label>
                            <input type="text" class="form-control" id="sspwd" placeholder="请输入连接密码（仅支持 英文+数字）">
                        </div>
                    </div>
                    <div><span><code>注意：修改连接密码后各服务器数据同步可能不及时，可能会导致某些服务节点连接不上，密码错误等(约5分钟时间全服务器同步)。</code></span></div><br/>
                    <button class="btn btn-info btn-block" id="submit-sspwd">确定修改</button>
                </div>
            </div>
        </div>
        <script>
            $(document).ready(function(){
                $("#submit-pwd").click(function(){
                    var but = $(this);
                    var nowpwd = $("#nowpwd").val();
                    var pwd = $("#pwd").val();
                    var repwd = $("#repwd").val();
                    if(nowpwd == null || nowpwd == '') {
                        showToastr("旧密码不能为空", 0, 2000);
                        return;
                    }
                    if(pwd == null || pwd == '' || repwd ==null || repwd =='') {
                        showToastr("喂喂，新密码不能为空诶！", 0, 2000);
                        return;
                    }
                    if(pwd != repwd && pwd != null && repwd != null) {
                        showToastr("两次密码不一致", 0, 2000);
                        return;
                    }
                    but.attr('disabled', 'true').addClass('disabled');
                    $.ajax({
                        type:"POST",
                        url:"/member/changePassWord.json",
                        data:{
                            'nowpwd': nowpwd,
                            'pwd': pwd,
                            'repwd': repwd
                        },
                        dataType:"json",
                        success:function(result) {
                            but.removeAttr('disabled').removeClass('disabled');
                            if(result.code != 200) {
                                showToastr(result.message, 0, 5000, '系统错误');
                                return;
                            }
                            if(result.data.error == 0) {
                                setTimeout("window.location.href='/auth/login'", 3000);
                            }
                            showToastr(result.data.message, 0,5000);
                        },
                        error:function(jqXHR){
                            but.removeAttr('disabled').removeClass('disabled');
                            showToastr("发生错误："+jqXHR.status, 0);
                        }
                    })
                });
                $("#submit-sspwd").click(function(){
                    var but = $(this);
                    var sspwd = $("#sspwd").val();
                    if(sspwd == null || sspwd == '') {
                        /*
                         showToastr("Shadowsocks连接密码不能为空", 0, 2000);
                         return;
                         */
                        var flags = confirm("如果连接密码留空，将再一次随机生成连接密码，请问真的要这么做吗？");
                        if(!flags) {
                            return;
                        }
                        sspwd = '1';
                    }
                    but.attr('disabled', 'true').addClass('disabled');
                    $.ajax({
                        type:"POST",
                        url:"/member/changeSSPwd.json",
                        data:{
                            'sspwd': sspwd,
                        },
                        dataType:"json",
                        success:function(result){
                            if(result.code != 200) {
                                showToastr(result.message, 0, 5000, '系统错误');
                                return;
                            }
                            if(result.data.error == 0) {
                                $("#old_sspwd").val(result.data.sspwd);
                            }

                            but.removeAttr('disabled').removeClass('disabled');
                            showToastr(result.data.message, 0,5000);
                        },
                        error:function(jqXHR){
                            showToastr("发生错误："+jqXHR.status, 0);
                            but.removeAttr('disabled').removeClass('disabled');
                        }
                    })
                });
            });
        </script>
        <import template="/panel/_copyright" />
    </div>
</div>
<import template="/panel/_footer" />